﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls;
using Eloje_Data;
using eLoje_Model;

namespace ELoje_web
{
    public partial class DepartamentoList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                gvDepartamento.PageIndex = 0;
                gvDepartamento.PageSize = 100;
                loadGrid();
                applyText();
                navigationHistory();
                
            }
        }

       protected void lnlnkEnableDisable_Click (object sender, CommandEventArgs e)
        {
           
        }

       protected void lnkAlterar_Click (object sender, CommandEventArgs e)
       {
           try
           {
               int codDepartamento = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
               string descDepartamento = e.CommandArgument.ToString().Split(';')[1];
               string status = e.CommandArgument.ToString().Split(';')[2];
               string sigla = e.CommandArgument.ToString().Split(';')[3];

               if (Session["codDepartamento"] != null)
                   Session.Remove("codDepartamento");
               if (Session["descDepartamento"] != null)
                   Session.Remove("descDepartamento");
               if (Session["Status"] != null)
                   Session.Remove("Status");
               if (Session["Sigla"] != null)
                   Session.Remove("Sigla");

               Session["codDepartamento"] = codDepartamento;
               Session["descDepartamento"] = descDepartamento;
               Session["Status"] = status;
               Session["Sigla"] = sigla;

               Server.Transfer("~/Cadastros/DepartamentoNew.aspx");
           }
           catch (Exception)
           {
               
               throw;
           }
       }

       protected void lnkLog_Click(object sender, CommandEventArgs e)
       {
           try
           {
               int codDepartamento = Convert.ToInt32(e.CommandArgument.ToString());
               

               if (Session["codDepartamento"] != null)
                   Session.Remove("codDepartamento");
               
               Session["codDepartamento"] = codDepartamento;
               
               Server.Transfer("~/Logs/DepartamentoLog.aspx");
           }
           catch (System.Threading.ThreadAbortException)
           { }
           catch (Exception)
           {

               throw;
           }
       }

       protected void gvDepartamento_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType .Equals(DataControlRowType.DataRow))
           {
               if(((DepartamentoVO)e.Row.DataItem).STATUS.Equals("S"))
               {
                   ((LinkButton)e.Row.Cells[3].FindControl("lnkEnableDisable")).Text = "Desabilitar";
               }

               if(((DepartamentoVO)e.Row.DataItem).STATUS.Equals("N"))
               {
                   ((LinkButton)e.Row.Cells[3].FindControl("lnkEnableDisable")).Text = "Habilitar";
               }
           }

       }

       protected void gvDepartamento_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {
           gvDepartamento.PageIndex = e.NewPageIndex;
           loadGrid();
       }

       protected void gvDepartamento_RowCreated(object sender, GridViewRowEventArgs e)
       {
            if(e.Row.RowType.Equals(DataControlRowType.DataRow))
            {
                LinkButton a = (LinkButton)e.Row.FindControl("lnkAlterar");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(a);
                LinkButton d = (LinkButton)e.Row.FindControl("lnkEnableDisable");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(d);
                LinkButton l = (LinkButton)e.Row.FindControl("lnkLog");
                ScriptManager.GetCurrent(this).RegisterPostBackControl(l);

            }
       }

        protected void btnAddNewDepartamento_Click (object sender, EventArgs e)
       {
           Response.Redirect("~/Cadastros/DepartamentoNew.aspx");
       }
        #region Métodos
        private void loadGrid()
        {
            try
            {
                DepartamentoDA da = new DepartamentoDA();
                gvDepartamento.DataSource = da.getDepartamento();
                gvDepartamento.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void applyText()
        {
            btnAddNewDepartamento.Text = "Novo Departamento";
        }

        private void navigationHistory()
        {
            lblHistoryNavigation.Text = String.Format("{0} >> {1} >> ", "Cockpit", "Cadastros");
            lblThisPage.Text = "Departamentos"; 

        }
        #endregion

        protected void lnkEnableDisable_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int codDepartamento = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
                string status = e.CommandArgument.ToString().Split(';')[1];
                string descDepartamento = e.CommandArgument.ToString().Split(';')[2];
                string sigla = e.CommandArgument.ToString().Split(';')[3];

                DepartamentoDA da = new DepartamentoDA();

                if (status.ToUpper().Equals("S"))
                    da.setDisableDepartamento(codDepartamento, descDepartamento, sigla, "Desenv");
                else
                    da.setEnableDepartamento(codDepartamento, descDepartamento, sigla, "Desenv");

                loadGrid();
            }
            catch (Exception)
            {

                throw;
            }
        }
                             
    }
}